﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ECI.CommonLibrary;
using System.Data;
using System.Web;
using ECI.Entities;
using ECI.Controls;
using ECI.CoreLibrary;
using ECI.WebLib.Data;

namespace ECI.DynamicData.ElementBuilder
{
    public class TDate : IElement
    {

        private TextBox txtBox = null;

        public override void Create(ElementParameter param)
        {
            this.Init(param);
            this.CreateContainer();

            txtBox = new TextBox();
            txtBox.Attributes.Add("sql", param.Field.SqlAttribute);
            txtBox.Attributes.Add("mainalias", param.Field.MainAlias);
            txtBox.Attributes.Add("maxlength", "20");

            //2010-07-14 增加控件宽度属性控制 控件的显示宽度
            if (param.type == "Edit")
            {
                if (param.Field.ControlWidth.Length > 0)
                {
                    txtBox.Style["width"] = param.Field.ControlWidth;
                }
                else
                {
                    txtBox.Style["width"] = "85%";//占整个单元格的85%
                }
            }
            else
            {
                txtBox.Style["width"] = "85%";//占整个单元格的85%
            }

            txtBox.ID = "txt" + param.Field.Name;
            if (param.EditMode == "BatchUpdate") txtBox.ID = "txtBatch" + param.Field.Name;

            string TypeConfig = param.Field.DictionaryTypeConfig;
            if (TypeConfig == "")
            {
                TypeConfig = "yyyy-MM-dd";
            }

            if (param.type.ToLower() == "query")
            {
                TypeConfig = "yyyy-MM-dd";
            }

            //txtBox.Attributes.Add("onfocus", "new WdatePicker(this,'" + TypeConfig + "'," + f + ");");
            txtBox.Attributes.Add("onfocus", "WdatePicker({dateFmt:'" + TypeConfig + "'})");
            txtBox.CssClass = "Wdate";

            //


            txtBox.AutoCompleteType = AutoCompleteType.Disabled;
            //if (param.type == "Edit") DynamicQuery.EditLockControl(txtBox, param.DataRow, param.Field, param.EditMode);
            //if (param.type == "Edit") DynamicQuery.EditDefaultControl(txtBox, param.DataRow, param.Field);
            //if (param.type == "Edit" && param.Field.SystemData.ToMyString() != "") DynamicQuery.LockControl(txtBox);

            if (param.type == "Edit" && DynamicQuery.EditHideForSystemField(param.Field))
            {
                param.HideCell.Controls.Add(txtBox);
            }
            else
            {
                MyTableCell.Controls.Add(txtBox);//新增文本框控件
                param.TableRow.Cells.Add(MyTableCell);
            }



            if (param.DataRow != null)
            {
                txtBox.Text = param.DataRow[param.Field.Name].ToString();
            }

            #region 复制新增时 是否清除控件内容
            if (param.Field.CopyRemoveData.ToMyString() == "1" && param.EditMode.ToLower() == "copy")
            {
                txtBox.Text = SQLParamConvert.Convert(Field.DefaultValue);
            }
            #endregion

        }

        #region IElement 成员


        public override void Locked()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;

            if (Field.EditLockMode == "LOCKFULL") LockControl();
            else if (dataRow != null && editMode.ToLower() != "copy" && Field.EditLockMode == "LOCKEDIT") LockControl();
            else if (dataRow == null && Field.EditLockMode == "LOCKADD") LockControl();
        }

        public override void Default()
        {
            DataRow dataRow = param.DataRow;
            if (Field.DefaultValue != "" && dataRow == null)
            {
                txtBox.Text = GetDefaultDataForControl();
            }
        }

        public override void SystemField()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;
            txtBox.ReadOnly = true;
            txtBox.BackColor = System.Drawing.Color.Gainsboro;
            if (txtBox.CssClass == "Wdate") txtBox.Attributes.Remove("onfocus");
            if (txtBox is ECIChoose) ((ECIChoose)txtBox).IsGeneric = true;

            if (Field.SystemData.ToMyString() != "" && Field.SystemDataTrigger.ToMyString() == "INSERT" && Field.SystemDataShow.ToMyString() == "1" && dataRow == null)
            {
                txtBox.Text = GetSystemDataValue();
            }
        }

        public override void Cascade()
        {
            DataRow dataRow = param.DataRow;
            if (dataRow == null)
            {
                string value = PageHelper.Request(Field.Name);
                if (value != "")
                {
                    txtBox.Text = value;
                }
            }
        }

        #endregion


        private void LockControl()
        {
            txtBox.ReadOnly = true;
            txtBox.BackColor = System.Drawing.Color.Gainsboro;
            if (txtBox.CssClass == "Wdate")
            {
                txtBox.Attributes.Remove("onclick");
                txtBox.Attributes.Remove("onfocus");
            }
            if (txtBox is ECIChoose) ((ECIChoose)txtBox).IsGeneric = true;
        }

        public override string GetValue()
        {
            string value = "";

            if (EditMode == DynamicEditMode.BatchUpdate)
            {
                CheckBox chk = param.Table.FindControl("chk" + Field.Name) as CheckBox;
                if (chk.Checked)
                {
                    TextBox txtBox = param.Table.FindControl("txtBatch" + Field.Name) as TextBox;

                    value = txtBox.Text;

                    return DynamicQuery.GetDynamicSQL(Field, value, DynamicEditMode.Update);

                }
                else
                {
                    return "";
                }

            }
            else
            {
                if (param.Field.SystemData.ToMyString() == "")
                {
                    TextBox txtBox = param.Table.FindControl("txt" + Field.Name) as TextBox;
                    value = txtBox.Text;
                }

                else
                {
                    value = this.GetSystemDataValue();
                }

                return DynamicQuery.GetDynamicSQL(Field, value, EditMode);
            }

        }


        public override void DataKeep()
        {
        }

        public override void QueryDefault()
        {
            if (Field.DefaultValueQuery != "" && param.type.ToLower() == "query")
            {
                string xs = Field.DefaultValueQuery;
                if (xs.IndexOf('}') != -1)
                {
                    xs = xs.Replace("{", "");
                    xs = xs.Replace("}", "");
                    txtBox.Text = DateTime.Now.ToString(xs);

                }
                else
                    txtBox.Text = SQLParamConvert.Convert(Field.DefaultValueQuery);
            }

            if (Field.Udf6 == "Y")
                LockControl();

        }

        public override void ClientEventRegister()
        {

        }

        public override string GetRealValue()
        {
            throw new NotImplementedException();
        }
    }
}
